﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.eforceglobal.crux.configuration;
using com.eforceglobal.crux.configuration.DTOs;
using System.Web;
using System.Net.Mail;

namespace com.eforceglobal.crux.notifications
{
    public class MailMan
    {
        public static void SendMail(string ActionName, List<KeyValuePair<string, string>> TagsAndValues, List<string> Receipients)
        {
            EmailNotificationActionInformation notificationInformation =
            CruxConfigurationManager.GetEmailNotificationSettings(ActionName);
            
            if (OkToSendEmail(notificationInformation))
            {
                string Host = notificationInformation.EmailNotificationInformation.SmtpServer;
                string UserName = notificationInformation.EmailNotificationInformation.UserName;
                string Password = notificationInformation.EmailNotificationInformation.EmailPassword;
                string FromAddress = notificationInformation.EmailNotificationInformation.FromAddress;
                bool UseSSL = notificationInformation.EmailNotificationInformation.UseSSL;
                int Port = notificationInformation.EmailNotificationInformation.Port;

                SmtpClient smtpClient = new SmtpClient(Host);
                smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
                smtpClient.Port = Port;
                smtpClient.EnableSsl = UseSSL;
                MailMessage mailMessage = FormMail(FromAddress, Receipients, TagsAndValues, notificationInformation);
                smtpClient.Send(mailMessage);
            }

        }

        public static MailMessage FormMail(string FromAddress, List<string> Receipients, List<KeyValuePair<string,string>> TagsAndValues, EmailNotificationActionInformation emailNotificationActionInformation)
        {
            string subject = ReplaceTagsInSubject(TagsAndValues, emailNotificationActionInformation);
            string body = ReplaceTagsInBody(TagsAndValues, emailNotificationActionInformation);
            MailAddress fromAddress = new MailAddress(FromAddress);
            MailMessage mailMessage = new MailMessage();
            bool IsBodyHtml = emailNotificationActionInformation.EmailNotificationInformation.SendInHtml;
            mailMessage.IsBodyHtml = IsBodyHtml;
            foreach (string recepient in Receipients)
            {
                mailMessage.To.Add(recepient);
            }
            
            mailMessage.From = fromAddress;
            mailMessage.Subject = subject;
            mailMessage.Body = body;
            return mailMessage;
        }

        private static bool OkToSendEmail(EmailNotificationActionInformation emailNotificationActionInformation)
        {
            return emailNotificationActionInformation.Notify &&
                   emailNotificationActionInformation.EmailNotificationInformation.Notify;
        }

        private static string ReplaceTagsInSubject(List<KeyValuePair<string,string>> TagsAndValues, EmailNotificationActionInformation notificationInformation)
        {
            string subject = notificationInformation.MailSubject;
            foreach (KeyValuePair<string,string> keyValue in TagsAndValues)
            {
                subject = subject.Replace(Constants.PlaceHolderOpenBracket + keyValue.Key + Constants.PlaceHolderClosingBracket, keyValue.Value);
            }
            return subject;
        }

        public static string ReplaceTagsInBody(List<KeyValuePair<string, string>> TagsAndValues, string  ActionName)
        {
            EmailNotificationActionInformation notificationInformation =
            CruxConfigurationManager.GetEmailNotificationSettings(ActionName);
            return ReplaceTagsInBody(TagsAndValues, notificationInformation);
        }


        public static string ReplaceTagsInSubject(List<KeyValuePair<string, string>> TagsAndValues, string ActionName)
        {
            EmailNotificationActionInformation notificationInformation =
            CruxConfigurationManager.GetEmailNotificationSettings(ActionName);
            return ReplaceTagsInSubject(TagsAndValues, notificationInformation);
        }

        private static string ReplaceTagsInBody(List<KeyValuePair<string, string>> TagsAndValues, EmailNotificationActionInformation emailNotificationInformation)
        {
            string TemplateFilePath = GetTemplatePath(emailNotificationInformation);
            string body = System.IO.File.ReadAllText(TemplateFilePath);
            foreach (KeyValuePair<string, string> keyValue in TagsAndValues)
            {
                body = body.Replace(Constants.PlaceHolderOpenBracket + keyValue.Key + Constants.PlaceHolderClosingBracket, keyValue.Value);
            }
            return body;
        }

        private static string GetTemplatePath(EmailNotificationActionInformation notificationInformation)
        {
            string ApplicationPath = CruxConfigurationManager.GetApplicationPath();
            string TemplatePath = ApplicationPath + "/"
                + notificationInformation.EmailNotificationInformation.TemplateFolder
                + "/" + notificationInformation.NotificationTemplateFile;
            string TemplateDiskPath = HttpContext.Current.Server.MapPath(TemplatePath);
            return TemplateDiskPath;
        }
    }
}
